<script setup>
import { ref, onMounted } from 'vue'
import throttle from '@/utils/throttle.js'

// 图片列表
defineProps({
  imageList: {
    type: Array,
    default() {
      return [
        'https://yanxuan-item.nosdn.127.net/d917c92e663c5ed0bb577c7ded73e4ec.png',
        'https://yanxuan-item.nosdn.127.net/e801b9572f0b0c02a52952b01adab967.jpg',
        'https://yanxuan-item.nosdn.127.net/b52c447ad472d51adbdde1a83f550ac2.jpg',
        'https://yanxuan-item.nosdn.127.net/f93243224dc37674dfca5874fe089c60.jpg',
        'https://yanxuan-item.nosdn.127.net/f881cfe7de9a576aaeea6ee0d1d24823.jpg',
      ]
    },
  },
})

// 图片预览功能
const currentIndex = ref(0) // 当前图片的下标
const onMouseEnter = i => {
  currentIndex.value = i
}

// 放大镜功能
const target = ref(null) // 获取大图盒子元素
const layer = ref(null) // 获取遮罩层元素
const large = ref(null) // 获取大图元素
const isShow = ref(false) // 控制是否显示大图的开关

onMounted(() => {
  target.value.addEventListener('mouseenter', () => {
    isShow.value = true
    target.value.addEventListener('mousemove', e => {
      throttle(handleMousemove, 10, e)
    })
  })
  target.value.addEventListener('mouseleave', () => {
    isShow.value = false
    target.value.removeEventListener('mousemove', e => {
      throttle(handleMousemove, 10, e)
    })
  })
})
// 鼠标移动事件的处理函数
const handleMousemove = function (e) {
  const rect = target.value.getBoundingClientRect()
  const rectLayer = layer.value.getBoundingClientRect()
  // 计算出鼠标在盒子内的坐标
  const x = e.clientX - rect.left
  const y = e.clientY - rect.top
  // 计算出遮罩层的top和left值
  let left = x - rectLayer.width / 2
  let top = y - rectLayer.height / 2
  // 计算出遮罩层的最大移动距离
  const maxX = rect.width - rectLayer.width
  const maxY = rect.height - rectLayer.height
  // 限制遮罩层的移动范围
  left < 0 && (left = 0)
  left > maxX && (left = maxX)
  top < 0 && (top = 0)
  top > maxY && (top = maxY)
  // 将left和top绑定到遮罩层的样式上
  layer.value.style.left = left + 'px'
  layer.value.style.top = top + 'px'

  // 计算大图的移动距离和小图移动距离的比例
  const largeWidth = parseInt(
    getComputedStyle(large.value)['background-size'].split(' ')[0]
  )
  const rate = largeWidth / rect.width
  const largeX = left * rate
  const largeY = top * rate
  large.value.style.backgroundPosition = `-${largeX}px -${largeY}px`
}
</script>

<template>
  <div class="goods-image">
    <!-- 左侧大图-->
    <div class="middle" ref="target">
      <img :src="imageList[currentIndex]" alt="" />
      <!-- 蒙层小滑块 -->
      <div class="layer" ref="layer" v-show="isShow"></div>
    </div>
    <!-- 小图列表 -->
    <ul class="small">
      <li
        v-for="(img, i) in imageList"
        :key="i"
        @mouseenter="onMouseEnter(i)"
        :class="{ active: i === currentIndex }"
      >
        <img :src="img" alt="" />
      </li>
    </ul>
    <!-- 放大镜大图 -->
    <div
      class="large"
      ref="large"
      :style="[
        {
          backgroundImage: `url(${imageList[currentIndex]})`,
        },
      ]"
      v-show="isShow"
    ></div>
  </div>
</template>

<style scoped lang="scss">
.goods-image {
  width: 480px;
  height: 400px;
  position: relative;
  display: flex;

  .middle {
    width: 400px;
    height: 400px;
    background: #f5f5f5;
  }

  .large {
    position: absolute;
    top: 0;
    left: 412px;
    width: 400px;
    height: 400px;
    z-index: 500;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    background-repeat: no-repeat;
    // 背景图:盒子的大小 = 2:1  将来控制背景图的移动来实现放大的效果查看 background-position
    background-size: 800px 800px;
    background-color: #f8f8f8;
  }

  .layer {
    width: 200px;
    height: 200px;
    background: rgba(0, 0, 0, 0.2);
    // 绝对定位 然后跟随咱们鼠标控制left和top属性就可以让滑块移动起来
    left: 0;
    top: 0;
    position: absolute;
  }

  .small {
    width: 80px;

    li {
      width: 68px;
      height: 68px;
      margin-left: 12px;
      margin-bottom: 15px;
      cursor: pointer;

      &:hover,
      &.active {
        border: 2px solid $xtxColor;
      }
    }
  }
}
</style>
